﻿#region 
/*---------------------------------------------------------------- 
* 项目名称 ：MRC.Domain.IService 
* 项目描述 ： 
* 类 名 称 ：IRoleService 
* 类 描 述 ： 
* 所在的域 ：DESKTOP-HKKGEJ7
* 命名空间 ：MRC.Domain.IService
* 机器名称 ：DESKTOP-HKKGEJ7 
* CLR 版本 ：4.0.30319.42000 
* 作 者 ：mrc 
* 创建时间 ：2018/11/2 23:23:54 
* 更新时间 ：2018/11/2 23:23:54 
* 版 本 号 ：v1.0.0.0 
******************************************************************* 
* Copyright @ mrc 2018. All rights reserved. *******************************************************************
 //----------------------------------------------------------------*/
#endregion

using Chloe;
using MRC.AutoMapper;
using MRC.Data.Models;
using MRC.Entity;
using MRC.Service.Application;
using MRC.ToolsAndEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MRC.Domain.IService
{
    public interface IRoleService : IAppService
    {
        List<Sys_Role> GetRoles(string keyword = "");
        List<SimpleRoleModel> GetSimpleModels();
        void Add(AddRoleInput input);
        void Update(UpdateRoleInput input);
        void Delete(string roleId, string operatorId);

        List<Sys_Role> GetList(string keyword = "");
        List<Sys_RolePermission> GetPermissions(string id);
        void SetPermission(string id, List<string> permissionList);
    }

    public class RoleService : AppServiceBase, IRoleService
    {
        public RoleService(IDbContext dbContext, IServiceProvider services) : base(dbContext, services)
        {
        }

        public List<Sys_Role> GetRoles(string keyword = "")
        {
            var q = this.DbContext.Query<Sys_Role>().FilterDeleted();
            if (keyword.IsNotNullOrEmpty())
            {
                q = q.Where(a => a.Name.Contains(keyword));
            }

            var ret = q.OrderBy(a => a.SortCode).ToList();
            return ret;
        }
        public List<SimpleRoleModel> GetSimpleModels()
        {
            var q = this.DbContext.Query<Sys_Role>().FilterDeletedAndDisabled().OrderBy(a => a.SortCode);
            var ret = q.Select(a => new SimpleRoleModel() { Id = a.Id, Name = a.Name }).ToList();
            return ret;
        }
        public void Add(AddRoleInput input)
        {
            input.Validate();
            Sys_Role role = this.CreateEntity<Sys_Role>(null, input.CreateUserId);

            MrcMapper.Map(input, role);
            this.DbContext.DoWithTransaction(() =>
            {
                this.DbContext.Insert(role);
            });
        }
        public void Update(UpdateRoleInput input)
        {
            input.Validate();

            Sys_Role role = this.DbContext.QueryByKey<Sys_Role>(input.Id, true);

            role.NotNull("角色不存在");

            MrcMapper.Map(input, role);

            this.DbContext.DoWithTransaction(() =>
            {
                this.DbContext.Update(role);
            });
        }

        public void Delete(string id, string operatorId)
        {

            this.SoftDelete<Sys_Role>(id, operatorId);
        }


        public List<Sys_Role> GetList(string keyword = "")
        {
            var q = this.DbContext.Query<Sys_Role>().FilterDeleted();
            q = q.Where(a => a.Name.Contains(keyword));
            var ret = q.OrderBy(a => a.SortCode).ToList();
            return ret;
        }
        public List<Sys_RolePermission> GetPermissions(string id)
        {
            return this.DbContext.Query<Sys_RolePermission>().Where(t => t.RoleId == id).ToList();
        }
        public void SetPermission(string id, List<string> permissionList)
        {
            id.NotNullOrEmpty();

            List<Sys_RolePermission> roleAuths = permissionList.Select(a => new Sys_RolePermission()
            {
                Id = IdHelper.CreateStringSnowflakeId(),
                RoleId = id,
                PermissionId = a
            }).ToList();

            this.DbContext.DoWithTransaction(() =>
            {
                this.DbContext.Delete<Sys_RolePermission>(a => a.RoleId == id);
                this.DbContext.InsertRange(roleAuths);
            });
        }
    }

}
